package com.wswy.wzcx.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.util.Log;
import com.che.libcommon.utils.RxBus;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendAuth.Resp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.wswy.wzcx.model.resp.WXLoginResp;
import com.wswy.wzcx.module.InternalMsgCode;
import com.wswy.wzcx.utils.ShareHelper;

/**
 * Created by wangduheng26 on 2017/7/14.
 * wangduheng26@gmail.com
 */

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

  private static final String TAG = "WXEntryActivity";

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    handleIntent();
  }

  private void handleIntent() {
    try {
      if (!ShareHelper.getWXApi().handleIntent(getIntent(), this)) {
        finish();
      }
    } catch (Exception e) {
      e.printStackTrace();
      finish();
    }
  }

  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent();
  }


  @Override
  public void onReq(BaseReq baseReq) {

  }

  @Override
  public void onResp(BaseResp resp) {
//    CcCenter.get().send(ShareHelper.WX_SHARE_HANDLE_RESULT, resp);

    Log.e(TAG, "onResp -->errCode:"+resp.errCode+"  "+resp.getType());


    switch (resp.getType()){
      case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:
        break;
      case ConstantsAPI.COMMAND_SENDAUTH:

        if (resp.errCode == 0 && resp instanceof SendAuth.Resp){

          SendAuth.Resp r = (Resp) resp;
          RxBus.getDefault().postWithCode(InternalMsgCode.CODE_WX_LOGIN,new WXLoginResp(resp.errCode,r.code,r.state));
        }else {
          RxBus.getDefault().postWithCode(InternalMsgCode.CODE_WX_LOGIN,new WXLoginResp(resp.errCode,null,null));
        }


        break;
    }

    finish();
  }

}
